﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace Kay.BaseLibrary.Helpers
{
    /// <summary>
    /// 加密帮助器
    /// </summary>
    public static class EncryptHelper
    {
        private static byte[] GetBytesMd5(string value)
        {
            var bytes = Encoding.Default.GetBytes(value);
            byte[] bytesMd5;
            using (var md5 = new MD5CryptoServiceProvider())
            {
                bytesMd5 = md5.ComputeHash(bytes);
            }
            return bytesMd5;
        }

        /// <summary>
        /// 使用 MD5 加密
        /// </summary>
        /// <param name="value">需要加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string ByMd5(string value)
        {
            var bytesMd5 = GetBytesMd5(value);
            var md5Value = BitConverter.ToString(bytesMd5);
            return md5Value;
        }

        /// <summary>
        /// 使用 MD5 加密并去掉 - 字符
        /// </summary>
        /// <param name="value">需要加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string ByMd5_1(string value)
        {
            var md5Value = ByMd5(value);
            md5Value = md5Value.Replace("-", "");
            return md5Value;
        }
    }
}